// const htp = 'http://ysf.etonepay.com:888';
const htp = "http://ceshi4.sdykt.com.cn:1280/ysy";
const app = getApp();
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function checkPhone(t) {
  if (!(/^1(3|4|5|7|8)\d{9}$/.test(t))) {
    return false;
  }
  return true;
}

function httServer(port, jsonParm, callback) {
  var dic = jsonParm;
  var sdic = Object.keys(dic).sort();
  var tempstr = '';
  for (var ki in sdic) {
    console.log(sdic[ki] + ":" + dic[sdic[ki]] + ",");
    tempstr += sdic[ki] + "=" + dic[sdic[ki]] + '&';
  }
  tempstr = tempstr.replace(/\s+/g, "");
  tempstr = tempstr.substring(0, tempstr.length - 1)
  console.log(tempstr);
  wx.request({
    url: htp + port,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    data: tempstr,
    success(res) {
      var rdta = res.data;
      var rcode = res.statusCode;
      if(rcode == '200'){
        if (rdta.code == '0000') {
          callback('', rdta);
        } else {
          callback(rdta.msg);
        }
      }else{
        callback('请求失败,返回:'+rcode);
      }
     
    },
    fail(e) {
      callback('请求失败')
    }
  })
}
//带token请求
function serverToken(port, jsonParm, callback) {
  var dic = jsonParm;
  var sdic = Object.keys(dic).sort();
  var tempstr = '';
  for (var ki in sdic) {
    console.log(sdic[ki] + ":" + dic[sdic[ki]] + ",");
    tempstr += sdic[ki] + "=" + dic[sdic[ki]] + '&';
  }
  tempstr = tempstr.replace(/\s+/g, "");
  tempstr = tempstr.substring(0, tempstr.length - 1)
  console.log(tempstr);
  wx.request({
    url: htp + port,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded' ,// 默认值
      'auth_token':app.globalData.token
    },
    data: tempstr,
    success(res) {
      var rdta = res.data;
      var rcode = res.statusCode;
      if (rcode == '200') {
        if (rdta.code == '0000') {
          callback('', rdta);
        } else {
          callback(rdta.msg);
        }
      } else {
        callback('请求失败,返回:' + rcode);
      }
    },
    fail(e) {
      callback('请求失败')
    }
  })
}
//用户登录状态判断
function userStatus(rcode){
  var result = '';
   if(rcode =='10'){
     result = '去认证';
   }else if(rcode == '20'){
     result = '待核实';
   }else if(rcode == '30'){
     result = '待认证';
   }else if(rcode == '40'){
     result = '开通绑定'
   }else if(rcode == '50'){
     result = '锁定用户'
   }else if(rcode == '60'){
     result = '已认证';
   }else if(rcode == '99'){
     result = '注销用户'
   }else{
     result = '状态异常';
   }
   return result
}
//用户等级
function userlevel(rcode){
  var result = '';
  if (rcode == '100001'){
    result = '普通用户'
  } else if (rcode =='100002'){
    result = '高级会员 '
  }else{
    result = '至尊会员'
  }
  return result;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  checkPhone: checkPhone,
  httServer: httServer,
  serverToken: serverToken,
  userStatus: userStatus,
  userlevel: userlevel,
}